package com.boot.web.serviceImpl;

import com.boot.web.dao.DepartmentDao;
import com.boot.web.dao.UserDao;
import com.boot.web.entity.Department;
import com.boot.web.entity.User;
import com.boot.web.service.DepartmentService;
import com.boot.web.vo.DepartmentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boot.constant.Constants;
import com.boot.base.serviceImpl.BaseServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * 部门信息业务层接口实现
 * @author 【zhouJie】
 * @date 2020-01-14 17:13:41
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public List<Department> getParentDepartment() {
        return departmentDao.queryParentDepartment();
    }

    @Override
    public List<DepartmentVO> getChildrenDepartment(Integer parentId) {
        Department department = departmentDao.queryById(parentId);
        List<DepartmentVO> departmentVOList = departmentDao.queryChildrenDepartment(parentId);
        if (departmentVOList.size() > 0) {
            for (DepartmentVO vo : departmentVOList) {
                vo.setParentTitle(department.getTitle());
            }
        }
        return departmentVOList;
    }

    @Override
    public int saveDepartment(Department department) {
        if (department.getParentId() != null) {
            Department department1 = departmentDao.queryById(department.getParentId());
            if (!department1.getIsParent()) {
                department1.setIsParent(true);
                departmentDao.update(department1);
            }
        }
        department.setUpdateTime(new Date());
        return departmentDao.save(department);
    }

    @Override
    public int updateDepartment(Department department) {
        int indexRow = 0;
        Department oldDepartment = departmentDao.queryById(department.getId());
        Integer oldParentId = 0;
        if (oldDepartment.getParentId() != null) {
            oldParentId = oldDepartment.getParentId();
        }
        department.setUpdateTime(new Date());
        indexRow = departmentDao.update(department);
        if (department.getParentId() != null) {
            Department newDepartment = departmentDao.queryById(department.getParentId());
            Integer newParentId = 0;
            if (newDepartment.getId() != null) {
                newParentId = newDepartment.getId();
            }
            if (oldParentId != newParentId) {
                newDepartment.setIsParent(true);
                newDepartment.setUpdateTime(new Date());
                indexRow = departmentDao.update(newDepartment);
                Department department1 = departmentDao.queryById(oldParentId);
                if(department1 != null){
                    if (departmentDao.queryChildrenDepartment(department1.getId()).size() <= 0) {
                        department1.setIsParent(false);
                        department1.setUpdateTime(new Date());
                        indexRow = departmentDao.update(department1);
                    }
                }
            }
        }
        return indexRow;
    }

    @Autowired
    private UserDao userDao;

    @Override
    public boolean batchDeleteDepartment(Integer[] ids) {
        if (ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                Department g = departmentDao.queryById(ids[i]);
                if (g != null) {
                    List<User> userList = userDao.queryUserByDepartmentId(g.getId());
                    if (userList.size() > 0) {
                        for (User user : userList) {
                            user.setDepartmentId(Constants.default_department);
                            userDao.update(user);
                        }
                    }
                    if (g.getParentId() == null) {
                        List<DepartmentVO> departmentList1 = departmentDao.queryChildrenDepartment(g.getId());
                        if (departmentList1.size() > 0) {
                            for (DepartmentVO vo : departmentList1) {
                                if (vo != null) {
                                    Department department = new Department();
                                    BeanUtils.copyProperties(vo, department);
                                    department.setParentId(Constants.default_department);
                                    departmentDao.update(department);
                                }
                            }
                        }
                    } else {
                        Department g1 = departmentDao.queryById(g.getParentId());
                        if (g1 != null) {
                            List<DepartmentVO> departmentVOList = departmentDao.queryChildrenDepartment(g1.getId());
                            if (departmentVOList.size() > 1) {
                                List<DepartmentVO> departmentVOList1 = departmentDao.queryChildrenDepartment(g.getId());
                                if (departmentVOList1.size() > 0) {
                                    for (DepartmentVO vo : departmentVOList1) {
                                        if (vo != null) {
                                            Department department = new Department();
                                            BeanUtils.copyProperties(vo, department);
                                            department.setParentId(Constants.default_department);
                                            departmentDao.update(department);
                                        }
                                    }
                                }
                            } else {
                                g1.setIsParent(false);
                                departmentDao.update(g1);
                            }
                        }
                    }
                }
            }
        }
        departmentDao.batchDelete(ids);
        return true;
    }

}
